package org.example.controller;

import jakarta.annotation.Resource;
import org.example.bean.Book;
import org.example.service.BookService;
import org.example.util.JsonResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookCotroller {
    @Resource
    private BookService bookService;

    @GetMapping
    public List<Book> find(){
        return bookService.list();
    }
    @GetMapping("/page/{page}")
    public JsonResult find(@PathVariable("page") int page){
        return bookService.findBook(page);
    }
    @GetMapping("/id/{id}")
    public JsonResult findById(@PathVariable("id")String id){
        return bookService.findBookById(id);
    }
    @GetMapping("/search/{name}/{page}")
    public JsonResult findBookByname(@PathVariable("name")String name,@PathVariable("page") int page){
        return bookService.findBookByname(name,page);
    }

    @PostMapping
    public JsonResult save(Book book){
        return bookService.saveBook(book);
    }
    @PutMapping
    private JsonResult update(Book book){
        return bookService.updateBook(book);
    }
    @PutMapping("/change_status/{id}/{status}")
    public JsonResult update(@PathVariable("id")String id,@PathVariable("status")int status){
        return bookService.updateBookStatus(id,status);
    }
    @DeleteMapping("/{id}")
    public JsonResult remove(@PathVariable("id")String id){
        return bookService.removeBook(id);
    }
}
